<?php
defined('BASEPATH') or exit('No direct script access allowed');
session_start();

class Subscriptions extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper("url");
        $this->load->model("subscription_model");
        $this->load->library("pagination");
        $this->load->helper("uuid");
        $this->load->helper("date");
    }

    private function initPagination($cur_page = 0, $param = NULL)
    {
        $total = $this->subscription_model->getCount($param);

        $config["base_url"] = "/" . site_url("subs");
        $config["total_rows"] = $total;
        $config["per_page"] = 10;
        $config['num_links'] = 2;
        $config["cur_page"] = $cur_page;
        $config["reuse_query_string"] = TRUE;

        $config['first_link'] = 'First';
        $config["first_tag_open"] = "<li class=\"paginate_button page-item previous\">";
        $config["first_tag_close"] = "</li>";

        $config['last_link'] = 'Last';
        $config["last_tag_open"] = "<li class=\"paginate_button page-item next\" id=\"datatable_next\">";
        $config["last_tag_close"] = "</li>";

        $config['cur_tag_open'] = '<li class="paginate_button page-item active"><a class="page-link">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="paginate_button page-item">';
        $config['num_tag_close'] = '</li>';

        $config['attributes'] = array('class' => 'page-link');

        $config["next_link"] = FALSE;
        $config["prev_link"] = FALSE;


        $this->pagination->initialize($config);
    }

    public function index($cur_page = 0)
    {
        if (!isset($_SESSION["user"])) {
            redirect("login");
            return;
        }
        $data["title"] = "公众号管理";

        $this->initPagination($cur_page, $_GET);

        $rows = $this->subscription_model->getAll($_GET, $cur_page, 10);
        $data["subs"] = $rows;

        $user = $_SESSION["user"];
        $data["nickname"] = $user["nickname"];

        $data["search"] = $_GET;

        $this->load->view("templates/header", $data);
        $this->load->view("subscriptions/subscriptions", $data);
        $this->load->view("templates/footer");
    }

    public function del($id = 0)
    {
        if (!isset($_SESSION["user"])) {
            redirect("login");
            return;
        }

        $this->subscription_model->del($id);

        redirect("subs");
    }

    public function create()
    {
        if (!isset($_SESSION["user"])) {
            redirect("login");
            return;
        }

        $data = array(
            "id" => create_uuid(),
            "sub_input_time" => date("Y-m-d H:i:s", now()),
            "sub_status" => 2
        );
        $sub_id = $this->input->post("sub_id");
        $sub_name = $this->input->post("sub_name");

        if ($sub_id) {
            $data["sub_id"] = $sub_id;
        }
        if ($sub_name) {
            $data["sub_name"] = $sub_name;
        }

        $this->subscription_model->create($data);
        redirect("subs");
    }

}